<?php

namespace Biz\model;


/**
 *
 * @author lee
 *
 */

use Biz\model\ShopBasicInfo as mShopBasicInfo;
use Doraemon\tools\ImgUrl as toolImgUrl;
use Biz\validate\ShopIdentifyImage as vShopIdentifyImage;


class ShopIdentifyImage extends \Doraemon\pockets\datebase\BaseDbModel
{

    const _CONFIG_FILE_ = 'db_shop';

    const _CONFIG_SELECT_ = 'db_shop_info';

    const _DATABASE_ = 'shop_info';

    const _TABLE_NAME = 'shop_identify_image';


    private static $_supportKeyCompanyMap = [
        'business_license' => '营业执照',
        'legal_person_positive' => '法人身份证正面',
        'legal_person_opposite' => '法人身份正反面',

    ];

    private static $_supportKeyCompanyOtherMap = [
        'trademark_certificate' => '商标注册证书',
        'brand_authorization' => '品牌授权书',
    ];

    private static $_supportKeyPersonalMap = [
        'id_number_positive' => '身份证正面',
        'id_number_opposite' => '身份证反面',
        'handheld_id_card' => '手持身份证',
    ];

    private static $_supportKeyPersonalOtherMap = [
        'sales_license' => '销售授权',
    ];

    private static $_supportKeyOtherMap = [

        'inspection_report' => '质检报告',

        'inspection_quarantine_report' => '检验检疫报告',
        'health_permit' => '卫生许可证',


    ];

    private static $_supportKeyBankMap = [
        'bank_account_certificate' => '银行开户证明'
    ];

    public static function getSupportKey()
    {
        $keyMap = array_merge(
            self::$_supportKeyPersonalMap,
            self::$_supportKeyCompanyMap,
            self::$_supportKeyCompanyOtherMap,
            self::$_supportKeyPersonalOtherMap,
            self::$_supportKeyOtherMap,
            self::$_supportKeyBankMap
        );

        return array_keys($keyMap);
    }

    public static function getImgList($uid, $shopType, $cate = ['basic'])
    {
        $data = [];
        $list = [];
        if (in_array('basic', $cate)) {
            switch ($shopType) {
                case mShopBasicInfo::TYPE_COMPANY:
                    $list['basic'] = self::$_supportKeyCompanyMap;
                    break;
                case mShopBasicInfo::TYPE_PERSONAL:
                    $list['basic'] = self::$_supportKeyPersonalMap;
                    break;
            }
        }

        if (in_array('other', $cate)) {
            switch ($shopType) {
                case mShopBasicInfo::TYPE_COMPANY:
                    $list['other'] = array_merge(self::$_supportKeyCompanyOtherMap, self::$_supportKeyOtherMap);
                    break;
                case mShopBasicInfo::TYPE_PERSONAL:
                    $list['other'] = array_merge(self::$_supportKeyPersonalOtherMap, self::$_supportKeyOtherMap);
                    break;
            }
        }

        if (in_array('bank', $cate)) {
            switch ($shopType) {
                case mShopBasicInfo::TYPE_COMPANY:
                    $list['bank'] = self::$_supportKeyBankMap;
                    break;
                case mShopBasicInfo::TYPE_PERSONAL:
                    $list['bank'] = self::$_supportKeyBankMap;
                    break;
            }
        }

        if (empty($list)) return $list;

        foreach ($list as $cate => $value) {
            $keys = array_keys($value);

            $res = self::select(['key', 'url_hash'], ['uid' => $uid, 'key' => $keys]);

            $data[$cate] = array_map(function ($v) use ($value) {
                $v['name'] = $value[$v['key']];

                self::formatInfo($v);

                return $v;
            }, $res);

        }


        return $data;
    }

    public static function formatInfo(&$info)
    {

        if ($info['url_hash']) {
            if (strpos($info['url_hash'], ',') === false) {
                $info['url'][] = [
                    'hash' => $info['url_hash'],
                    'thumb' => toolImgUrl::getWaterUrl(toolImgUrl::APPTYPE_BAUTH, $info['url_hash'], toolImgUrl::IMAGE_SIZE_SQ_THUMB),
                    'url' => toolImgUrl::getWaterUrl(toolImgUrl::APPTYPE_BAUTH, $info['url_hash'], toolImgUrl::IMAGE_SIZE_ORI_PHONE_BIG)
                ];
            } else {
                $urlHashs = explode(',', $info['url_hash']);
                foreach ($urlHashs as $hash) {
                    $info['url'][] = [
                        'hash' => $hash,
                        'thumb' => toolImgUrl::getWaterUrl(toolImgUrl::APPTYPE_BAUTH, $hash, toolImgUrl::IMAGE_SIZE_SQ_THUMB),
                        'url' => toolImgUrl::getWaterUrl(toolImgUrl::APPTYPE_BAUTH, $hash, toolImgUrl::IMAGE_SIZE_ORI_PHONE_BIG)
                    ];
                }
            }
        } else {
            $info['url'] = [];
        }

        unset($info['url_hash']);
    }


    public static function createOrEdit($uid, $data, $scenes = null, $method = null)
    {

        try {

            if (!isset($data['key']) || empty($data['key']))
                throw new \Exception('请从正确渠道填写');

            if (is_null($method)) {
                $has = self::has_master(['uid' => $uid, 'key' => $data['key']]);

                $method = $has ? 'update' : 'insert';
            }


            if (!is_null($scenes)) {
                //提交字段进行校验
                if (!vShopIdentifyImage::validate($data, $scenes)) {

                    $msg = vShopIdentifyImage::firstError();

                    throw new \Exception($msg);
                }
            }


            $data['url_hash'] = !empty($data['url_hash']) ? $data['url_hash'] : '';


            switch ($method) {
                case 'insert':
                    $data['shop_id'] = $uid;
                    $data['uid'] = $uid;
                    $data['addtime'] = date('Y-m-d H:i:s');

                    $ret = self::insert($data);
                    break;
                case 'update':
                    $ret = self::update($data, ['uid' => $uid, 'key' => $data['key']]);
                    break;
            }


            if ($ret->errorCode() !== '00000') {
                throw new \Exception('数据库操作错误');
            }

            return [$method => $ret->rowCount()];


        } catch (\Exception $e) {


            throw $e;
        }

    }

}

